package com.zzyl.nursing.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.vo.DeviceDataVo;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 设备管理Controller
 * 
 * @author ych
 * @date 2025-10-17
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备管理的接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;
    /**
     * 查看设备详情
     */
    @ApiOperation("查看设备详情")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/{iotId}")
    public AjaxResult deviceDetail(@PathVariable("iotId") String iotId) {
        DeviceDetailVo deviceDetailVo = deviceService.deviceDetail(iotId);
        return AjaxResult.success(deviceDetailVo);
    }

    @ApiOperation("删除设备")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @DeleteMapping("/{iotId}")
    public AjaxResult deleteDevice(@PathVariable("iotId") String iotId) {

       deviceService.deleteDevice(iotId);
        return AjaxResult.success();
    }


    ;




    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @PutMapping
    public AjaxResult editDeviceInfo(@PathVariable DeviceDto dto) {

       deviceService.editDeviceInfo(dto);
        return AjaxResult.success(dto);
    }


    @ApiOperation("查询设备列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping ("/queryServiceProperties/{iotId}")
    public AjaxResult queryServiceProperties(@PathVariable("iotId") String iotId) {

 List<DeviceDataVo> deviceDetailVo    =  deviceService.queryServiceProperties(iotId);
        return AjaxResult.success(deviceDetailVo);
    }


    @ApiOperation("查询所有产品列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @PostMapping ("/register")
    public AjaxResult register(@RequestBody DeviceDto dto) {

        deviceService.register(dto);
        return AjaxResult.success();
    }

    @ApiOperation("查询所有产品列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping ("/allProduct")
    public AjaxResult allProduct() {

    List<ProductVo> productVoList =    deviceService.allProduct();
        return AjaxResult.success(productVoList);
    }

    @ApiOperation("从物联网平台同步产品列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @PostMapping ("/syncProductList")
    public AjaxResult syncProductList() {

   deviceService.syncProductList();
        return AjaxResult.success();
    }

    /**
     * 查询设备管理列表
     */
    @ApiOperation("查询设备管理列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }
    @ApiOperation("查询设备上报的最新数据")
    @PreAuthorize("@ss.hasPermi('nursing:alertRule:list')")
    @GetMapping("/queryProduct/{productKey}")
    public AjaxResult queryProductById(@PathVariable("productKey") String productKey)
    {


        return deviceService.queryProductById(productKey);
    }
}